package com.qen.plugin.notification;

import com.qen.App;
import com.qen.fx.ViewIndex;
import com.qen.plugin.enums.NotificationPosition;
import com.qen.plugin.enums.NotificationType;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


public class Notification {

    private String title = "提示";
    private String content = "这是一条提示消息";
    private Image close = new Image(App.class.getResourceAsStream("icon/plugin/close.png"));
    private Image success = new Image(App.class.getResourceAsStream("icon/plugin/success.png"));
    private Image info = new Image(App.class.getResourceAsStream("icon/plugin/info.png"));
    private Image error = new Image(App.class.getResourceAsStream("icon/plugin/error.png"));
    private Image warning = new Image(App.class.getResourceAsStream("icon/plugin/warning.png"));
    private boolean showIcon = false;
    private boolean showClose = true;
    private NotificationPosition position = NotificationPosition.TOP_RIGHT;
    private NotificationType type = NotificationType.NONE;
    private int timeout = 3000;
    private boolean autoClose = true;

    public Notification(String title, String content, Image close, Image success, Image info, Image error, Image warning, boolean showIcon, boolean showClose, NotificationPosition position, NotificationType type, int timeout, boolean autoClose) {
        this.title = title;
        this.content = content;
        this.close = close;
        this.success = success;
        this.info = info;
        this.error = error;
        this.warning = warning;
        this.showIcon = showIcon;
        this.showClose = showClose;
        this.position = position;
        this.type = type;
        this.timeout = timeout;
        this.autoClose = autoClose;
    }

    public Notification(String title, String content, NotificationType type) {
        this.title = title;
        this.content = content;
        this.type = type;
    }

    public Notification() {
    }

    public Notification(String content) {
        this.content = content;
    }

    public void showNatification() {
        VBox vBox = new VBox();
        vBox.setPrefWidth(ViewIndex.NotificationPreferHeight);
        if (position == NotificationPosition.TOP_LEFT) {
            vBox.setTranslateX(20);
            vBox.setTranslateY(20);
        } else if (position == NotificationPosition.BOTTOM_LEFT) {
            vBox.setTranslateX(20);
            double translateY = ViewIndex.WindowHeight - 100;
            vBox.setTranslateY(translateY);
        } else if (position == NotificationPosition.BOTTOM_RIGHT) {
            double translateX = ViewIndex.WindowWidth - ViewIndex.NotificationPreferHeight + 20;
            vBox.setTranslateX(translateX);
            double translateY = ViewIndex.WindowHeight - 100;
            vBox.setTranslateY(translateY);
        } else {
            double translateX = ViewIndex.WindowWidth - ViewIndex.NotificationPreferHeight - 20;
            vBox.setTranslateX(translateX);
            vBox.setTranslateY(20);
        }
        vBox.setStyle("-fx-border-radius: 10px; -fx-border-width: 1px; -fx-border-color: #fff; -fx-border-insets: 5px;-fx-box-border: true;-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.5), 10, 0.0, 0, 0);-fx-background-radius: 10px; -fx-background-color: #FFF;");
        vBox.setId("notification");

        ImageView imageView = null;
        if (NotificationType.SUCCESS == type) {
            imageView = new ImageView(success);
        } else if (NotificationType.ERROR == type) {
            imageView = new ImageView(error);
        } else if (NotificationType.WARNING == type) {
            imageView = new ImageView(warning);
        } else if (NotificationType.INFO == type) {
            imageView = new ImageView(info);
        } else {

        }
        if (imageView != null) {
            imageView.setFitHeight(20);
            imageView.setFitWidth(20);
            imageView.setPickOnBounds(true);
            imageView.setPreserveRatio(true);
            HBox.setMargin(imageView, new Insets(0, 10, 0, 0));
        }
        Label label = new Label(title);
        label.setFont(new Font("System Bold", 15));
        label.setPrefWidth(300);
        HBox.setMargin(label, new Insets(0, 0, 10, 0));

        ImageView imageViewClose = null;
        if (showClose) {
            imageViewClose = new ImageView(close);
            imageViewClose.setCursor(Cursor.HAND);
            imageViewClose.setOnMouseClicked(e -> {
                App.rootPane.getChildren().remove(vBox);
            });
            imageViewClose.setId("natificationClose");
            imageViewClose.setFitWidth(15);
            imageViewClose.setFitHeight(15);
            imageViewClose.setPickOnBounds(true);
            imageViewClose.setPreserveRatio(true);
        }
        HBox hBox = new HBox();
        if (imageView != null) {
            hBox.getChildren().add(imageView);
        }
        hBox.getChildren().add(label);
        if (imageViewClose != null) {
            hBox.getChildren().add(imageViewClose);
        }
        Label con = new Label(content);
        con.setWrapText(true);
        VBox.setMargin(con, new Insets(0, 0, 10, 0));
        vBox.getChildren().add(hBox);
        vBox.getChildren().add(con);
        vBox.setPadding(new Insets(10, 20, 0, 20));
        App.rootPane.getChildren().add(vBox);
        if (autoClose) {
            ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
            // 延迟1秒后执行任务
            executor.schedule(() -> {
                vBox.setManaged(false);
                vBox.setVisible(false);
                vBox.setDisable(false);
            }, timeout, TimeUnit.MILLISECONDS);
            // 关闭executor
            executor.shutdown();
        }
    }


}
